import com.dmotiko.zonaoutlet.*
import com.dmotiko.general.*
import com.dmotiko.util.*
import mx.utils.Delegate;

class ZOSite
extends WebSite {
	
	public static var HOME:String = "zo_home";
	public static var QUIENES_SOMOS:String = "zo_quienessomos";
	public static var BUSCADOR:String = "zo_buscador";
	public static var PROMOCIONES:String = "zo_promociones";
	public static var VIP:String = "zo_vip";
	public static var ATENCION:String = "zo_atencion";
		
	private var navBar:ZONavBarTop;
	private var navBarLeft:ZONavBarLeft;
		
	private var typesLoader:DataXMLLoader;
	private var typesData:Array;
	
	private var outletsLoader:OutletsLoader;
	private var outletsData:Array;
	
	private var promoLoader:PromoLoader;
	private var promoData:Array;
		
	/* SECTIONS */
	private var mcl:MovieClipLoader;
	private var mcHome:MovieClip;
	private var mcQuienesSomos:MovieClip;
	private var mcBuscador:MovieClip;
	private var mcPromociones:MovieClip;
	private var mcVIP:MovieClip;
	private var mcAtencion:MovieClip;
	private var mcNavBar:MovieClip;
	private var mcNavBarLeft:MovieClip;
	
	private var activeSection:MovieClip;
	private var activeOutlet:Object;
	
	private var nTotalBytes:Number;
	private var oLoadedBytes:Object;
		
	
	public static function init(timeline:MovieClip){
		if(!app) app = new ZOSite(timeline);
	}
	
	public static function getApp():ZOSite {
		return ZOSite(app);
	}
	
	private function ZOSite(timeline){
		super(timeline);
		initSite();
	}
	
	private function initSite(){
		this.sSection = ZOSite.HOME;
		initStage();
		loadExternalData();
	}
	
	private function initStage():Void {
		Stage.align = "T";
		Stage.scaleMode = "noScale";
	}
	
	private function loadExternalData():Void {
		outletsLoader = new OutletsLoader();
		outletsLoader.run("outlets.xml");
		outletsLoader.addEventListener( DataXMLLoader.DATA_LOADED, Delegate.create(this, outletsLoaded) );
	}
	
	private function outletsLoaded( evnt ) {
		outletsData = evnt.data;
		promoLoader = new PromoLoader();
		promoLoader.run("promos.xml");
		promoLoader.addEventListener( DataXMLLoader.DATA_LOADED,  Delegate.create(this, promoLoaded) );
	}
	
	private function promoLoaded( evnt ) {
		promoData = evnt.data;
		typesLoader = new DataXMLLoader();
		typesLoader.run( "outletTypes.xml" );
		typesLoader.addEventListener( DataXMLLoader.DATA_LOADED, Delegate.create(this, typesLoaded) );
		
	}
	
	private function typesLoaded( evnt ):Void {
		typesData = evnt.data.type;
		loadComplete();
	}
	
	private function initLoader():Void {
		
	}
	
	private function loadComplete():Void {
		initSections();
	}
	private function initSections():Void {
		mcl = new MovieClipLoader();
		mcl.addListener( this );
		
		mcHome = this.timeline.createEmptyMovieClip("mcHome", this.timeline.getNextHighestDepth());
		mcQuienesSomos = this.timeline.createEmptyMovieClip("mcQuienesSomos", this.timeline.getNextHighestDepth());
		mcBuscador = this.timeline.createEmptyMovieClip("mcBuscador", this.timeline.getNextHighestDepth());
		mcPromociones = this.timeline.createEmptyMovieClip("mcPromociones", this.timeline.getNextHighestDepth());
		mcVIP = this.timeline.createEmptyMovieClip("mcVIP", this.timeline.getNextHighestDepth());
		mcAtencion = this.timeline.createEmptyMovieClip("mcAtencion", this.timeline.getNextHighestDepth());
		mcNavBar = this.timeline.attachMovie("ZONavBar", "mcNavBar", this.timeline.getNextHighestDepth(), { _x: 257, _y: 102 });
		mcNavBarLeft = this.timeline.attachMovie("menuLeftAppear", "mcNavBarLeft", this.timeline.getNextHighestDepth(), { _x: 70, _y: 146  } );
		
		mcl.loadClip("home.swf", mcHome);
		mcl.loadClip("quienesSomos.swf", mcQuienesSomos);
		mcl.loadClip("buscador.swf", mcBuscador);
		mcl.loadClip("promociones.swf", mcPromociones);
		mcl.loadClip("VIP.swf", mcVIP);
		mcl.loadClip("atencion.swf", mcAtencion);
	}
	
	private function onLoadStart( target:MovieClip ):Void {
		if ( !nTotalBytes ) nTotalBytes = 0;
		nTotalBytes += mcl.getProgress( target ).bytesTotal;
	}
	
	private function onLoadProgress( target:MovieClip, nLoaded:Number, nTotal:Number ):Void {
		if ( !oLoadedBytes ) oLoadedBytes = new Object();
		oLoadedBytes[target._name] = nLoaded;
		var n:Number = 0;
		for ( var sProp:String in oLoadedBytes) {
			n += oLoadedBytes[sProp];
		}
		trace( n + " bytes de " + nTotalBytes + " | " + Math.round( n * 100 / nTotalBytes ) );
	}
	
	private function onLoadInit( target ):Void {
		
	}
	
	public function setNavBar(navBar:ZONavBarTop):Void {
		this.navBar = navBar;
	}
	public function getNavBar():ZONavBarTop { 
		return this.navBar; 
	}
	
	public function setNavBarLeft(navBarLeft:ZONavBarLeft):Void {
		this.navBarLeft = navBarLeft;
	}
	public function getNavBarLeft():ZONavBarLeft { 
		return this.navBarLeft; 
	}
	
	public function getOutletsData(sType:String):Array { 
		if ( sType == "special" ) {
			var aData:Array = new Array();
			for (var i:Number = 0; i < outletsData.length; i++) {
				if ( outletsData[i].special ) aData.push( outletsData[i] );
			}
			return aData;
			
		} else if ( sType ) {
			var aData:Array = new Array();
			for (var i:Number = 0; i < outletsData.length; i++) {
				var types:Array = String(outletsData[i].type).split(',');
				for (var i2:Number = 0; i2 < types.length; i2++) {
					if ( sType == 0 || types[i2] == sType ) {
						aData.push( { key: outletsData[i].label, value: outletsData[i] } );
						break;
					}
				}
			}
			//return aData;
			return aData.sortOn( ["key"], [Array.CASEINSENSITIVE] );
		}
		
		return outletsData; 
	}
	public function getPromosData():Array { 
		return promoData; 
	}
	public function getTypesData(bForCombo:Boolean):Array { 
		if (bForCombo) {
			var aData:Array = new Array();
			for (var i:Number = 0; i < typesData.length; i++) {
				if( getOutletsData( typesData[i].ID ).length > 0 ) aData.push( { key: typesData[i].name, value: typesData[i].ID } );
			}
			return aData;
		}
		return typesData; 
	}
	
	public function getActiveOutlet():Object { return activeOutlet; }
	
	public function setActiveOutlet(value:Object):Void {
		activeOutlet = value;
	}
	
}